package com.qc.dao.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qc.common.StringUtil;
import com.qc.dao.HeightDao;
import com.qc.entity.HeightEntity;

@Repository
public class HeightDaoImpl implements HeightDao{
    
    @Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    @Override
    public int getListHeightCount(String name) {
        String sql = "select count(*) from t_height where 1 = 1 and if_del = '0' ";
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        return jdbcTemplate.queryForInt(sql);
    }

    @Override
    public List<Map<String, Object>> getAllHeight(String name, int pageNo, int pageSize) {
        int beginRow = (pageNo - 1)* pageSize;
        String sql = "select id,name,price from t_height where 1 = 1 and if_del = '0'  ";
        
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        
        sql += " order by created_time DESC ";
        sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
        
        return jdbcTemplate.queryForList(sql);
    }

    @Override
    public int saveHeight(HeightEntity heightEntity) {
        String sql = "insert into t_height(name,price) values(?,?)";
        return jdbcTemplate.update(sql,new Object[]{heightEntity.getName(),heightEntity.getPrice()});
    }

    @Override
    public int deleteHeightId(long id) {
        
        String sql = "update t_height set if_del = '1' where id = " +id;
        return jdbcTemplate.update(sql);
    }

    @Override
    public int updateHeightId(HeightEntity heightEntity) {
       
        String sql = "update t_height set name = ? ,price=?  where id = ? ";
        return jdbcTemplate.update(sql,new Object[]{heightEntity.getName(),heightEntity.getPrice(),heightEntity.getId()});
    }

}
